|
|
|
|
|
|
|
If an API function expects a value passed by reference, you must use an intermediate variable to pass the property value to the function. For example, the GetClassName function returns a window's class name. The Visual Basic declaration for this function is: |
|
|
|
 |
|
|
|
|
Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) _
As Long |
|
|
|
|
|
|
|
|
While the string argument is declared ByVal, this actually passes to the function the address of the string's character data. Because this value is passed by reference, the following code does not correctly set a form's Caption to the form's class name. |
|
|
|
 |
|
|
|
|
Private Sub Form_Load()
Dim length As Long
' This does not work! length = GetClassName(hWnd, Caption, Len(Caption))
End Sub |
|
|
|
|
|
|
|
|
Instead, you need to use an intermediate variable to retrieve the class name and then assign it to the form's Caption property. |
|
|
|
 |
|
|
|
|
Private Sub Form_Load()
Dim length As Long
Dim txt As String * 256
' This version works. length = GetClassName(hWnd, txt, Len(txt))
Caption = Left$(txt, length)
End Sub |
|
|
|
|
|
|
|
|
Translating C into Visual Basic |
|
|
|
|
|
|
|
|
The API Viewer and the file Win32api.txt declare API types, constants, and functions for use by Visual Basic programs. The MSDN help and most other sources of API information describe API routines using C language syntax. To understand how to use these functions, it helps to be able to translate from C syntax into Visual Basic syntax. Table 2.1 shows C language declarations and their Visual Basic equivalents. Note that in 32-bit operating systems, the API functions assume integers are actually long integers. If you are working on a 32-bit operating system and a C language API declaration calls for an integer, use a long integer instead. |
|
|
|
|
|
|
|
|
Once you have defined any constants and data structures you need and you have declared the API function, you can call it as you would any Visual Basic function. The following code shows everything needed to determine the number of colors loaded into a form's color table if the computer is using color palettes. |
|
|
|
|
|